<template>
  <div class="info-edit-dialog" style="position: relative; margin-top: 10px;">
    <el-form
      ref="formEditOnlineFormDict"
      :model="formOnlineDict"
      class="full-width-input"
      :rules="rules"
      style="width: 98%;"
      label-width="120px"
      size="small"
      label-position="right"
      @submit.native.prevent
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="字典名称" prop="dictName">
            <el-input v-model="formOnlineDict.dictName" class="input-item" :clearable="true" placeholder="字典名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字典类型" prop="dictType">
            <el-select v-model="formOnlineDict.dictType" class="input-item" :clearable="true" :disabled="isEdit" placeholder="字典类型" :loading="dictTypeWidget.loading" @visible-change="dictTypeWidget.onVisibleChange" @change="onDictTypeChange">
              <el-option v-for="item in dictTypeWidget.dropdownList" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 静态字典选择 -->
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.STATIC" :span="12">
          <el-form-item label="字典选择" prop="staticDictName">
            <el-select v-model="formOnlineDict.staticDictName" class="input-item" :clearable="true" filterable placeholder="字典类型" :loading="staticDictWidget.loading" @visible-change="staticDictWidget.onVisibleChange" @change="onStaiicDictChange">
              <el-option v-for="item in staticDictWidget.dropdownList" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 数据表字典，选择数据表 -->
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="数据库" prop="dblinkId">
            <el-select v-model="formOnlineDict.dblinkId" class="input-item" :clearable="true" placeholder="数据表所属数据库" :loading="dblinkIdWidget.loading" @visible-change="dblinkIdWidget.onVisibleChange" @change="onDblinkChange">
              <el-option v-for="item in dblinkIdWidget.dropdownList" :key="item.dblinkId" :value="item.dblinkId" :label="item.dblinkName" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="数据表" prop="tableName">
            <el-select v-model="formOnlineDict.tableName" class="input-item" :clearable="true" placeholder="选择字典数据表" :loading="tableNameWidget.loading" filterable @visible-change="tableNameWidget.onVisibleChange" @change="onDictTableChange">
              <el-option v-for="item in tableNameWidget.dropdownList" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- URL字典，输入字典获取url -->
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.URL" :span="12">
          <el-form-item label="字典URL" prop="dictListUrl">
            <el-input v-model="formOnlineDict.dictListUrl" class="input-item" :clearable="true" placeholder="输入字典获取url" @change="clearDictInfo">
              <el-button slot="append" :size="defaultFormItemSize" @click="onGetDictData(true)">获取数据</el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.URL" :span="12">
          <el-form-item label="数据获取URL" prop="dictIdsUrl">
            <el-input v-model="formOnlineDict.dictIdsUrl" class="input-item" :clearable="true" placeholder="根据输入的ids参数，获取字典数据" />
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.URL || formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12" style="height: 47px;">
          <el-form-item label="树状字典">
            <el-switch v-model="formOnlineDict.treeFlag" />
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.treeFlag && (formOnlineDict.dictType === SysOnlineDictType.URL || formOnlineDict.dictType === SysOnlineDictType.TABLE)" :span="12">
          <el-form-item label="字典父字段">
            <el-select v-model="formOnlineDict.parentKeyColumnName" clearable placeholder="选择字典父字段" filterable allow-create default-first-option>
              <el-option v-for="column in dictColumnList" :key="column" :label="column" :value="column" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.URL || formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="字典键字段" prop="keyColumnName">
            <el-select v-model="formOnlineDict.keyColumnName" clearable placeholder="选择字典键字段" filterable allow-create default-first-option>
              <el-option v-for="column in dictColumnList" :key="column" :label="column" :value="column" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.URL || formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="字典值字段" prop="valueColumnName">
            <el-select v-model="formOnlineDict.valueColumnName" clearable placeholder="选择字典值字段" filterable allow-create default-first-option>
              <el-option v-for="column in dictColumnList" :key="column" :label="column" :value="column" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="逻辑删除字段">
            <el-select v-model="formOnlineDict.deletedColumnName" clearable placeholder="选择逻辑删除字段" filterable allow-create default-first-option>
              <el-option v-for="column in dictColumnList" :key="column" :label="column" :value="column" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="用户过滤字段">
            <el-select v-model="formOnlineDict.userFilterColumnName" clearable placeholder="选择用户过滤字段，用于数据权限用户过滤" filterable allow-create default-first-option>
              <el-option v-for="column in dictColumnList" :key="column" :label="column" :value="column" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="部门过滤字段">
            <el-select v-model="formOnlineDict.deptFilterColumnName" clearable placeholder="选择部门过滤字段，用于数据权限部门过滤" filterable allow-create default-first-option>
              <el-option v-for="column in dictColumnList" :key="column" :label="column" :value="column" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="租户过滤字段">
            <el-select v-model="formOnlineDict.tenantFilterColumnName" clearable placeholder="选择部门过滤字段，用于多租户租户过滤" filterable allow-create default-first-option>
              <el-option v-for="column in dictColumnList" :key="column" :label="column" :value="column" />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 字典参数 -->
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.URL || formOnlineDict.dictType === SysOnlineDictType.TABLE" :span="12">
          <el-form-item label="字典参数">
            <el-select v-model="dictParamList" placeholder="请添加字典获取参数" multiple filterable allow-create default-first-option>
              <el-option v-for="item in dictColumnList" :key="item" :value="item" :label="item" />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- 字典数据 -->
        <el-col v-if="formOnlineDict.dictType === SysOnlineDictType.STATIC || formOnlineDict.dictType === SysOnlineDictType.CUSTOM" :span="24">
          <div style="margin: 0 20px;width: 100%;">
            <el-table :data="dictData" size="small" header-cell-class-name="table-header-gray" height="300px">
              <el-table-column label="字典键数据" prop="id" />
              <el-table-column label="字典值数据" prop="name" />
              <el-table-column v-if="formOnlineDict.dictType === SysOnlineDictType.CUSTOM" label="操作" width="150px" fixed="right" align="right">
                <template slot="header">
                  <el-row type="flex" justify="end" align="middle">
                    <EditDictDataButton width="200px" btn-text="新建" @save="onAddDictData" />
                  </el-row>
                </template>
                <template slot-scope="scope">
                  <EditDictDataButton width="200px" size="small" :value="scope.row" btn-text="编辑" @save="onEditDictData" />
                  <el-button class="table-btn delete" size="small" style="margin-left: 10px;" type="text" @click="onDeleteDictData(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer btnList">
      <el-button type="primary" :size="defaultFormItemSize" :plain="true" @click="onCancel(false)">取消</el-button>
      <el-button type="primary" :size="defaultFormItemSize" @click="onSaveClick()">保存</el-button>
    </div>
  </div>
</template>

<script>
import '@/staticDict/flowStaticDict.js'
import * as StaticDict from '@/staticDict'
/* eslint-disable-next-line */
import { DropdownWidget } from '@/utils/widget.js'
/* eslint-disable-next-line */
import { OnlineDictController, OnlineDblinkController } from '@/api/onlineController.js'
import EditDictDataButton from './editDictDataButton.vue'

export default {
  name: 'FormEditOnlineDict',
  components: {
    EditDictDataButton
  },
  props: {
    dictId: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      formOnlineDict: {
        dblinkId: undefined,
        dictName: undefined,
        dictType: this.SysOnlineDictType.TABLE,
        dictListUrl: undefined,
        dictIdsUrl: undefined,
        staticDictName: undefined,
        tableName: undefined,
        // 字典父字段名称
        parentKeyColumnName: undefined,
        // 字典键字段名称
        keyColumnName: undefined,
        // 字典值字段名称
        valueColumnName: undefined,
        // 逻辑删除字段名称
        deletedColumnName: undefined,
        // 用户过滤字段名称
        userFilterColumnName: undefined,
        // 部门过滤字段名称
        deptFilterColumnName: undefined,
        // 租户过滤字段名称
        tenantFilterColumnName: undefined,
        // 树状字典
        treeFlag: false
      },
      dictData: [],
      // 字典字段列表
      dictColumnList: [],
      // 数据表所属数据库
      dblinkIdWidget: new DropdownWidget(this.loadDblinkWidgetDropdownList),
      // 字典数据表
      tableNameWidget: new DropdownWidget(this.loadTableNameWidgetDropdownList),
      // 字典类型
      dictTypeWidget: new DropdownWidget(this.loadDictTypeWidgetDropdownList),
      // 静态字典下拉选择
      staticDictWidget: new DropdownWidget(this.loadStaticDictWidgetDropdownList),
      // 当前选中字典参数
      currentParam: undefined,
      // 字典参数列表
      dictParamList: [],
      dictTableProps: {
        lazy: true,
        lazyLoad: this.loadDictTableData
      },
      rules: {
        'dictName': [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        'dblinkId': [
          { required: true, message: '请选择字典数据表所属数据库', trigger: 'blur' }
        ],
        'tableName': [
          { required: true, message: '请选择字典数据表', trigger: 'blur' }
        ],
        'dictListUrl': [
          { required: true, message: '请输入字典数据获取URL', trigger: 'blur' }
        ],
        'dictIdsUrl': [
          { required: true, message: '请输入字典指定数据获取URL', trigger: 'blur' }
        ],
        'staticDictName': [
          { required: true, message: '请选择静态字典', trigger: 'blur' }
        ],
        'keyColumnName': [
          { required: true, message: '请输入字典键字段名称', trigger: 'blur' }
        ],
        'valueColumnName': [
          { required: true, message: '请输入字典值字段名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isEdit() {
      return this.dictId != null && this.dcitId !== ''
    }
  },
  mounted() {
    // 获取字典类型下拉列表数据
    this.dictTypeWidget.onVisibleChange(true)
    // 编辑状态下获取字典参数列表
    if (this.isEdit) {
      OnlineDictController.view({
        dictId: this.dictId
      }).then(res => {
        this.formOnlineDict = {
          ...res.data
        }
        // 解析字典参数信息
        this.formOnlineDict.dictDataJson = undefined
        const dataJson = JSON.parse(res.data.dictDataJson)
        if (dataJson && Array.isArray(dataJson.paramList)) {
          this.dictParamList = dataJson.paramList.map(item => {
            return item.dictParamName
          })
        }
        // 获取字典字段
        if (this.formOnlineDict.dictType === this.SysOnlineDictType.TABLE) {
          // 获取下拉数据
          this.dblinkIdWidget.onVisibleChange(true)
          this.tableNameWidget.onVisibleChange(true)
          this.loadTableColumnList()
        } else if (this.formOnlineDict.dictType === this.SysOnlineDictType.URL) {
          this.onGetDictData()
        } else if (this.formOnlineDict.dictType === this.SysOnlineDictType.STATIC) {
          this.staticDictWidget.onVisibleChange(true)
          if (dataJson) this.formOnlineDict.staticDictName = dataJson.staticDictName
          this.dictData = StaticDict[this.formOnlineDict.staticDictName] ? StaticDict[this.formOnlineDict.staticDictName].getList() : []
        } else if (this.formOnlineDict.dictType === this.SysOnlineDictType.CUSTOM) {
          if (dataJson && Array.isArray(dataJson.dictData)) {
            this.dictData = dataJson.dictData
          }
        }
      }).catch(e => {})
    }
  },
  methods: {
    /**
     * 字典数据
     */
    onAddDictData(dictData) {
      if (dictData != null) this.dictData.push(dictData)
    },
    onEditDictData(newValue, oldValue) {
      if (newValue != null && oldValue != null) {
        this.dictData = this.dictData.map(item => {
          return item.id === oldValue.id ? newValue : item
        })
      }
    },
    onDeleteDictData(row) {
      if (row != null) {
        this.dictData = this.dictData.filter(item => {
          return item.id !== row.id
        })
      }
    },
    loadDblinkWidgetDropdownList() {
      return new Promise((resolve, reject) => {
        OnlineDblinkController.list({}).then(res => {
          return resolve(res.data.dataList)
        }).catch(e => {
          reject(e)
        })
      })
    },
    onDblinkChange(value) {
      this.clearDictInfo()
      this.formOnlineDict.tableName = undefined
      this.tableNameWidget.dirty = true
    },
    loadTableNameWidgetDropdownList() {
      if (this.formOnlineDict.dblinkId == null || this.formOnlineDict.dblinkId === '') return Promise.reject()
      return new Promise((resolve, reject) => {
        OnlineDblinkController.listDblinkTables({
          dblinkId: this.formOnlineDict.dblinkId
        }).then(res => {
          resolve(res.data.map(item => {
            return {
              id: item.tableName,
              name: item.tableName
            }
          }))
        }).catch(e => {
          reject(e)
        })
      })
    },
    /**
     * 选择字典数据表
     */
    onDictTableChange(value) {
      this.formOnlineDict.tableName = value
      if (this.formOnlineDict.tableName != null) {
        this.loadTableColumnList()
      }
    },
    /**
     * 清空字典信息
     */
    clearDictInfo() {
      this.formOnlineDict.parentKeyColumnName = undefined
      this.formOnlineDict.keyColumnName = undefined
      this.formOnlineDict.valueColumnName = undefined
      this.dictParamList = []
      this.formOnlineDict.staticDictName = undefined
      this.dictData = []
    },
    loadTableColumnList() {
      const params = {
        dblinkId: this.formOnlineDict.dblinkId,
        tableName: this.formOnlineDict.tableName
      }
      this.dictColumnList = []
      OnlineDblinkController.listDblinkTableColumns(params).then(res => {
        this.dictColumnList = res.data.map(item => {
          return item.columnName
        })
      }).catch(e => {})
    },
    onDictTypeChange() {
      this.clearDictInfo()
      this.formOnlineDict.dictListUrl = undefined
      this.formOnlineDict.tableName = undefined
      this.formOnlineDict.dblinkId = undefined
      this.dictColumnList = []
    },
    onStaiicDictChange() {
      const staticDict = StaticDict[this.formOnlineDict.staticDictName]
      this.dictData = []
      if (staticDict != null) {
        this.dictData = staticDict.getList()
      }
    },
    /**
     * 获取字典数据
     */
    onGetDictData(showWarning = false) {
      const url = this.formOnlineDict.dictListUrl
      if (url == null || url === '') {
        this.$message.error('请输入字典数据获取URL')
        return
      }
      this.dictColumnList = []
      this.doUrl(url, 'get', {}).then(res => {
        if (showWarning) {
          this.$message.success('获取字典数据成功！')
        }
        if (Array.isArray(res.data) && res.data.length > 0) {
          const dictDataItem = res.data[0]
          this.dictColumnList = Object.keys(dictDataItem)
        }
      }).catch(e => {})
    },
    /**
     * 获取字典类型下拉列表
     */
    loadDictTypeWidgetDropdownList() {
      return Promise.resolve(this.SysOnlineDictType.getList())
    },
    onCancel(isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess)
      }
    },
    /**
     * 获取静态字典下拉列表
     */
    loadStaticDictWidgetDropdownList() {
      return Promise.resolve(Object.keys(StaticDict).map(key => {
        // 过滤掉静态字典的基类
        if (key === 'DictionaryBase') return undefined
        return {
          id: key,
          name: StaticDict[key].showName
        }
      }).filter(item => item != null))
    },
    /**
     * 保存字典信息
     */
    onSaveClick() {
      this.$refs.formEditOnlineFormDict.validate((valid) => {
        if (!valid) return
        const params = {
          onlineDictDto: {
            ...this.formOnlineDict,
            dblinkId: this.formOnlineDict.dblinkId,
            dictDataJson: JSON.stringify({
              staticDictName: this.formOnlineDict.dictType === this.SysOnlineDictType.STATIC ? this.formOnlineDict.staticDictName : undefined,
              dictData: this.formOnlineDict.dictType === this.SysOnlineDictType.CUSTOM ? this.dictData : undefined,
              paramList: this.dictParamList.map(item => {
                return {
                  dictParamName: item
                }
              })
            })
          }
        }

        const httpCall = this.isEdit ? OnlineDictController.update(params) : OnlineDictController.add(params)
        httpCall.then(res => {
          this.$message.success('保存成功')
          this.onCancel(true)
        }).catch(e => {})
      })
    }
  }
}
</script>

<style  lang="scss">
  .info-edit-dialog {
    display: flex;
    flex-direction: column;
    margin: 10px 0;
    .blueLink {
      color: #409eff;
      font-size: 14px;
      margin-left: 20px;
      cursor: pointer;
    }
    .btnList {
      margin-top:20px;
      display: flex;
      justify-content: right;
      float: right;
      .submitBtn {
        margin-right: 30px;
      }
    }
    .fromFlex {
      height: 36px;
      display: flex;
      align-items: center;
    }
    .base-info {
      display: flex;
      margin-bottom: 20px;
      .basics-info {
        width: 100%;
      }
    }
  }
</style>
